﻿//右键菜单@ryh_20070606
/*
 cssFile:菜单模式css文件
 nomalClass:菜单子项的普通模式
 hotClass:菜单子项获得焦点模式
 menuClass:菜单模式
 menuWidth:菜单宽度
*/
PopMenu=function(cssFile,nomalClass,hotClass,menuClass,menuWidth,menuHeight){
	//初始化菜单式样
	this.nomalClass=nomalClass;
	this.hotClass=hotClass;
	this.menuClass=menuClass;
	this.menuWidth=menuWidth;
	this.menuHeight=menuHeight;
	this.cssFile=cssFile;

	this.menuItems=new Array();
	this.parentMenu=null;
	this.childMenus=new Array();
	this.hotChild=null;
	this.parentIndex=null;

	this.menuBody=null;//菜单体

    this.area = ( document.compatMode.toLowerCase()=="css1compat" ) ? document.documentElement : document.body; 
    
	this.top=null;
	this.left=null;
}
/*
//创建菜单
PopMenu.prototype.createMenu=function()
{
 this.menuItem=
 
 /*
 if(this.parentMenu!=null)
  this.menuBody=this.parentMenu.menuBody.document.parentWindow.createPopup();
 else
  this.menuBody=window.createPopup();
  
 
 this.menuBody.document.createStyleSheet(this.cssFile);
 
 this.menuTable=createTable(this.menuBody.document,this.menuClass);
    this.menuBody.document.oncontextmenu=function()
    {

            return false;

    }
    var pop=this.menuBody;
    var mt=this;
   this.menuBody.document.onclick=function()

    {
   clearSelMenu(mt);
            pop.hide();

    }

}*/

//创建子菜单,参数:子菜单父亲的index（用于定位),菜单宽度
PopMenu.prototype.createChildMenu=function(index,menuWidth,menuHeight){
	var newp=new PopMenu(this.cssFile,this.nomalClass,this.hotClass,this.menuClass,menuWidth,menuHeight);
	newp.parentMenu=this;
	newp.parentIndex=index;
	this.childMenus[this.childMenus.length]=newp;
	return newp;
}

//创建菜单项
PopMenu.prototype.addMenuItem=function(text,clickFunciton,menuId){
	var tindex=this.menuItems.length;
	var menu={index:tindex,menuId:menuId,text:text,clickF:clickFunciton};
	this.menuItems[tindex]=menu;
}

//显示菜单
PopMenu.prototype.showMenu=function(){
	this.createMenuBody();
	this.createMenuItem();
	var rowCount=this.menuTable.rows.length

	if(this.parentMenu==null){
		this.menuBody.show(event.clientX,event.clientY+this.area.scrollTop,this.menuWidth,(this.menuHeight+2)*rowCount,document.body);
	}else{
		this.menuBody.show(this.parentMenu.menuWidth,(this.menuHeight+2)*this.parentIndex+5,this.menuWidth,(this.menuHeight+2)*rowCount,this.parentMenu.menuBody.document.body);
	}
}

//创建菜单
PopMenu.prototype.createMenuBody=function(){
	if(this.parentMenu!=null){
		this.menuBody=this.parentMenu.menuBody.document.parentWindow.createPopup();
	}else{
		this.menuBody=window.createPopup();
	}

	this.menuBody.document.createStyleSheet(this.cssFile);

	this.menuTable=createTable(this.menuBody.document,this.menuClass);
	this.menuBody.document.oncontextmenu=function(){
		return false;
	}
	var pop=this.menuBody;
	var mt=this;
	this.menuBody.document.onclick=function(){
		clearSelMenu(mt);
		var mtp=mt.parentMenu;
		while(mtp!=null){
			mtp.menuBody.hide();
			mtp=mtp.parentMenu;
		}
		pop.hide();
	}
}

PopMenu.prototype.createMenuItem=function(){
	for(var i=0;i<this.menuItems.length;i++){
		var itemM=this.menuItems[i];
		var tr=this.menuBody.document.createElement("<tr></tr>");
		var td=this.menuBody.document.createElement("<td></td>");
		//td.height=this.menuHeight;
		if(itemM.text==""){
			td.className="hr";
			td.innerHTML="<hr>";
		}else{
			td.className=this.nomalClass;
			td.innerHTML=itemM.text;
		}

		var pop=this;
		td.id=i;

		td.onmouseover=function(){
			if(this.innerHTML!="<HR>") this.className=pop.hotClass;	   
			//显示子菜单
			if(pop.hotChild!=null) pop.hotChild.hide();
			for(var j=0;j<pop.childMenus.length;j++){
				if(pop.childMenus[j].parentIndex==this.id){
					//window.status=this.id;
					pop.childMenus[j].showMenu();//显示子菜单
					pop.hotChild=pop.childMenus[j];
					return;
				 }
			}
		}

		var pop2=this;

		td.onmouseout=function(){
			if(this.innerHTML!="<HR>") this.className=pop2.nomalClass;
		}

		if(itemM.clickF!=null){
			td.clickF=itemM.clickF;
		}else{
			//td.clickF="return false;";
		}
		td.onclick=function(){
			eval(this.clickF);
		};

		tr.appendChild(td);
		this.menuTable.appendChild(tr);
	}
}

//菜单隐藏
PopMenu.prototype.hide=function(){
	this.menuBody.hide();
}

//创建容纳菜单项的表格
function createTable(p,className){
	//var t=p.createElement('<table border="1" width="100%" height="100%" bgcolor="#ffffff" style="border:thin;font-size: 12px" cellspacing="0"  bordercolor="#336633" >');
	var t=p.createElement('<table cellpadding="0" cellspacing="2">');
	t.className=className;
	t.width="100%";
	t.height="100%";
	var tb=p.createElement("TBody");
	if(t!=null&&tb!=null);
		t.appendChild(tb);
	var d=p.createElement("<div></div>");
	d.appendChild(t);
	if(p!=null)
		p.body.appendChild(d);
	return tb;
}

//菜单复原，清除选择
clearSelMenu=function(pop){
	//循环设置每行的属性
	var mt=pop.menuTable;
	for(var i=0;i<mt.children.length;i++){
		mt.children[i].children[0].className=pop.nomalClass;
	}
}

//清除菜单项
PopMenu.prototype.clear=function(){
	this.menuItem.length=0;
	if(this.menuTable.children.length==0) return;
	for(var i=this.menuTable.children.length-1;i>=0;i--){
		this.menuTable.removeChild(this.menuTable.children[i]);
	}
}

//显示菜单
function showMenu(){
	popMenu.showMenu();
	return false;
}

//隐藏菜单
function hideMenu(){
	popMenu.hide();
	return false;
}

var popMenu;
document.oncontextmenu = showMenu;
document.body.onload = initPopmenu;
//document.body.onclick=hideMenu;